// components/date_picker/index.js
const mybehavior = require("../../behaviors/requests")
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [mybehavior],
  properties: {
    date_picker: {
      type: String,
      value: ''
    },
    label: {
      type: String,
      value: ''
    },
    placeholder: {
      type: String,
      value: ''
    },

    minDate: {
      type: String,
      value: new Date().getTime()
    },
    date_type: {
      type: String,
      value: 'date'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    popup_date_show: false,
    currentDate: new Date().getTime(),
    minHour: 10,
    maxHour: 20,
    currentChoose: ""

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 确定开始日期输入框
    onDateConfirm(e) {
      if (this.data.date_type == 'date') {
        const currentChoose = this.formatDate1(new Date(e.detail))

        this.setData({
          popup_date_show: false,
          date_picker: currentChoose
        });
        this.triggerEvent('selectDate', currentChoose)
      } else {
        const currentChooseAll = this.formatTimestamp(new Date(e.detail))
        this.setData({
          popup_date_show: false,
          date_picker: currentChooseAll
        });
        this.triggerEvent('selectDateAll', currentChooseAll)
      }

    },
    // 开始日期输入框关闭
    onDateClose() {
      this.setData({
        popup_date_show: false,
      });
    },
    // 开始点击输入框显示选择日期
    onDateClick() {
      this.setData({
        popup_date_show: true,
      });
    },
    formatTimestamp(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      let str = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds

      return str;
    },

  }
})
